package com.hydl.power.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.CityMapper;
import com.hydl.power.mapper.DemadReportMapper;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.City;
import com.hydl.power.pojo.DemandReport;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.CityService;
import com.hydl.power.service.DemadReportService;
import com.hydl.power.utils.TableUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DemadReportServiceImpl implements DemadReportService {

    @Autowired
    private DemadReportMapper demadReportMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<List<DemandReport>> queryDemadReport(String startTime, String endTime, String token) {
        SysUser sysUser = sysUserMapper.getUserByToken(token);
        List<DemandReport> time=new ArrayList<>();
        List<DemandReport> arr=new ArrayList<>();
        //Page page = PageHelper.startPage(pageNum,pageSize);
        Map<String,List<DemandReport>> map=new LinkedHashMap<>();
        if (sysUser != null) {
            if (sysUser.getStatus() == 2) {

                //每月设备Pt最大值
                List<DemandReport> list = demadReportMapper.queryDemadReport(startTime, endTime,sysUser.getProjectId().toString());
                for (DemandReport demandReport : list) {
                    //使用查出来的最大值与id 查询数据时间
                    time=demadReportMapper.queryTime(demandReport.getDeviceId(),demandReport.getPt());
                    //arr作为map的value，如果里边值得id不存在，则清空arr list
                    for (DemandReport arrCount : arr) {
                        if(!arrCount.getDeviceId().equals(demandReport.getDeviceId())){
                            arr=new ArrayList<>();
                        }
                    }
                        // 将查出来的时间 赋值于最大值
                            demandReport.setTime(time.get(0).getTime());
                            //添加至list 集合(将要向map作为value进行添加)
                            arr.add(demandReport);

                    //判断map是否存在相同id，如果存在 清空map 重新put(list集合)，因为map会覆盖相同得key，所以先清空，再把相同得id存入一个list添加
                    if(map.containsKey(demandReport.getDeviceId())){
                        map.remove(demandReport.getDeviceId());
                        map.put(demandReport.getDeviceId(),arr);
                    }

                    //不存在正常添加，key为对象id
                    map.put(demandReport.getDeviceId(),arr);
                }
/*                TableUtils tu = new TableUtils();
                tu.setRows(map);
                tu.setTotal(page.getTotal());*/
                List<List<DemandReport>> arr2=new ArrayList<List<DemandReport>>(map.values());
                return arr2;
            }
        }
        //每月设备Pt最大值
        List<DemandReport> list = demadReportMapper.queryDemadReport(startTime, endTime,null);

        for (DemandReport demandReport : list) {
            //使用查出来的最大值与id 查询数据时间
            time=demadReportMapper.queryTime(demandReport.getDeviceId(),demandReport.getPt());
            //arr作为map的value，如果里边值得id不存在，则清空arr list
            for (DemandReport arrCount : arr) {
                if(!arrCount.getDeviceId().equals(demandReport.getDeviceId())){
                    arr=new ArrayList<>();
                }
            }
            // 循环将查出来的时间 赋值于最大值
            for (DemandReport report : time) {
                if(demandReport.getDeviceId().equals(report.getDeviceId())){
                    demandReport.setTime(report.getTime());
                    //添加至list 集合(将要向map作为value进行添加)
                    arr.add(demandReport);
                    break;
                }
            }
            //判断map是否存在相同id，如果存在 清空map 重新put(list集合)，因为map会覆盖相同得key，所以先清空，再把相同得id存入一个list添加
            if(map.containsKey(demandReport.getDeviceId())){
                map.remove(demandReport.getDeviceId());
                map.put(demandReport.getDeviceId(),arr);
            }
            //不存在正常添加，key为对象id
            map.put(demandReport.getDeviceId(),arr);
        }
        List<List<DemandReport>> arr2=new ArrayList<List<DemandReport>>(map.values());
        return arr2;
    }
}
